<template>
  <div class="past-exhibitions">
    <div
      class="page-header image"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
    >
      <div class="header-overlay">
        <div class="container">
          <h1 class="page-title">{{ "往届展会" }}</h1>
          <p class="page-subtitle">
            {{ pageData.subtitle || "回顾历届展会的精彩瞬间和成果" }}
          </p>
        </div>
      </div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('exhibition-intro')"
            :class="['nav-item', { active: activeTab === 'exhibition-intro' }]"
          >
            展会介绍
          </a>
          <a
            @click="setActiveTab('product-scope')"
            :class="['nav-item', { active: activeTab === 'product-scope' }]"
          >
            展品范围
          </a>
          <a
            @click="setActiveTab('exhibition-highlights')"
            :class="[
              'nav-item',
              { active: activeTab === 'exhibition-highlights' },
            ]"
          >
            展会集锦
          </a>
          <a
            @click="setActiveTab('venue-intro')"
            :class="['nav-item', { active: activeTab === 'venue-intro' }]"
          >
            展馆介绍
          </a>
          <a
            @click="setActiveTab('organizer-intro')"
            :class="['nav-item', { active: activeTab === 'organizer-intro' }]"
          >
            主办方介绍
          </a>
          <a
            @click="setActiveTab('organization-structure')"
            :class="[
              'nav-item',
              { active: activeTab === 'organization-structure' },
            ]"
          >
            组织架构
          </a>
          <a
            @click="setActiveTab('past-exhibitions')"
            :class="['nav-item', { active: activeTab === 'past-exhibitions' }]"
          >
            往届展会
          </a>
          <a
            @click="setActiveTab('downloads')"
            :class="['nav-item', { active: activeTab === 'downloads' }]"
          >
            下载中心
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <section class="intro-section" v-if="!pastExhibitions.length">
        <div class="intro-content">
          <div class="intro-text">
            <div v-if="pageData.imageUrl" class="intro-image">
              <img
                :src="pageData.imageUrl"
                :alt="pageData.title || '往届展会'"
              />
            </div>
            <div class="intro-description" v-html="pageData.content"></div>
          </div>
        </div>
      </section>

      <!-- 多数据：自上而下，图片在上，内容在下 -->
      <section v-if="pastExhibitions.length" class="exhibitions-list">
        <div
          class="exhibition-card"
          v-for="item in pastExhibitions"
          :key="item.id"
        >
          <div class="exhibition-image" v-if="item.image">
            <img :src="item.image" :alt="item.title" />
          </div>
          <div class="exhibition-body">
            <h3 class="exhibition-title">{{ item.title }}</h3>
            <div
              class="exhibition-rich"
              v-if="item.description"
              v-html="item.description"
            ></div>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { subpageApi } from "@/api/user";
import { selectLangField } from "@/utils/lang";

export default {
  name: "PastExhibitions",
  setup() {
    const activeTab = ref("past-exhibitions");
    const pageData = ref({});
    const bannerImage = ref("");
    const pastExhibitions = ref([]);

    const parseContent = (content) => {
      if (!content) return {};
      try {
        if (typeof content === "string" && content.trim().startsWith("{")) {
          return JSON.parse(content);
        }
        return { content: content };
      } catch (e) {
        console.log("解析content字段失败:", e);
        return { content: content };
      }
    };

    const loadPageData = async () => {
      try {
        const response = await subpageApi.getSubpageByKey(
          "about",
          "past-exhibitions"
        );
        if (response && response.data) {
          const parsed = parseContent(response.data.content);
          pageData.value = {
            title: response.data.title || "往届展会",
            subtitle: response.data.subtitle || "回顾历届展会的精彩瞬间和成果",
            content: parsed.content || response.data.content || "暂无内容",
            imageUrl: parsed.imageUrl || response.data.imageUrl || "",
          };
        }

        // 读取多条数据列表
        const listRes = await subpageApi.getSubpageList(
          "about",
          "past-exhibitions"
        );
        if (listRes && listRes.code === "200" && Array.isArray(listRes.data)) {
          pastExhibitions.value = listRes.data.map((it, idx) => {
            let parsed = {};
            try {
              parsed = it.content
                ? typeof it.content === "string"
                  ? JSON.parse(it.content)
                  : it.content
                : {};
            } catch {}
            return {
              id: it.id || idx + 1,
              title: selectLangField(
                parsed,
                "subtitle",
                selectLangField(
                  it,
                  "title",
                  parsed.subtitle || it.title || "往届展会"
                )
              ),
              description: selectLangField(parsed, "content", it.content || ""),
              image: selectLangField(
                parsed,
                "backgroundImage",
                it.imageUrl || ""
              ),
            };
          });
        }
      } catch (error) {
        console.error("加载往届展会数据失败:", error);
        pageData.value = {
          title: "往届展会",
          subtitle: "回顾历届展会的精彩瞬间和成果",
          content:
            "<p>FAIR plus机器人全产业链接会自2010年创办以来，已成功举办多届展会，每届都吸引了来自全球的顶尖机器人企业和专业观众。</p><p>历届展会见证了机器人产业的快速发展，从最初的工业机器人展示，到如今涵盖智能制造、人工智能、服务机器人等多个领域的综合性展览平台。</p><p>通过回顾往届展会的精彩瞬间，我们可以更好地了解行业发展趋势，为未来的展会提供宝贵的经验和参考。</p>",
        };
        pastExhibitions.value = [];
      }
    };

    const loadBanner = async () => {
      try {
        const resp = await subpageApi.getContentByType("carousel_about");
        if (
          resp &&
          resp.code === "200" &&
          Array.isArray(resp.data) &&
          resp.data.length
        ) {
          const first =
            resp.data.find((it) => it.status === "active") || resp.data[0];
          const content =
            typeof first.content === "string"
              ? JSON.parse(first.content || "{}")
              : first.content || {};
          bannerImage.value = first.imageUrl || content.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      // 跳转到对应的子页面
      if (tabKey !== "past-exhibitions") {
        window.location.href = `/${tabKey}`;
      }
    };

    onMounted(() => {
      loadPageData();
      loadBanner();
    });

    return {
      activeTab,
      pageData,
      bannerImage,
      pastExhibitions,
      setActiveTab,
    };
  },
};
</script>

<style scoped>
.past-exhibitions {
  min-height: 100vh;
  background: #f8f9fa; /* 与关于我们页面一致的浅灰背景 */
  /* padding-top: 70px; 与其他页面顶部间距一致，避免被导航遮挡 */
}
:deep(.resizable-image) {
  max-width: 100% !important;
  height: auto !important;
}

.page-header {
  background: linear-gradient(135deg, #320063 0%, #4a0a8a 100%);
  color: white;
  padding: 80px 0;
  text-align: center;
}

.page-header.image {
  position: relative;
  background: none;
  padding: 0;
  height: 400px;
  background-size: cover;
  background-position: center;
}
.header-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
}

.page-title {
  font-size: 48px; /* 与其他页面一致 */
  margin-bottom: 20px;
}

.page-subtitle {
  font-size: 18px; /* 与其他页面一致 */
  opacity: 0.9;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.intro-section {
  padding: 60px 0;
}

.intro-content {
  background: white;
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid #e8eaed;
}

.intro-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 30px;
}

.intro-image {
  width: 100%;
  max-width: 800px; /* 与文本宽度对齐 */
  border-radius: 12px;
  overflow: hidden;
}

.intro-image img {
  width: 100%;
  height: auto;
  display: block;
}

.intro-description {
  width: 100%;
  max-width: 800px;
  text-align: left;
  line-height: 1.8;
  color: #333;
  font-size: 1.05rem;
}

.intro-description :deep(p) {
  margin-bottom: 16px;
}

/* 富文本内图片限制 */
.intro-description :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 10px 0;
  border-radius: 8px;
}

.exhibitions-list {
  padding: 10px 0 40px;
}

.exhibition-card {
  background: #fff;
  border-radius: 16px;
  border: 1px solid #e8eaed;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.06);
  overflow: hidden;
  margin-bottom: 20px;
}

.exhibition-image {
  width: 100%;
  height: auto;
}

.exhibition-image img {
  width: 100%;
  height: auto;
  display: block;
}

.exhibition-body {
  padding: 18px 22px 22px;
}

.exhibition-title {
  margin: 0 0 10px 0;
  font-size: 1.2rem;
  font-weight: 600;
  color: #320063;
}

.exhibition-rich {
  color: #555;
  line-height: 1.7;
  white-space: normal;
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.exhibition-rich :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 8px 0;
  border-radius: 8px;
}
.exhibition-rich :deep(p) {
  margin-bottom: 12px;
}
.exhibition-rich :deep(ul),
.exhibition-rich :deep(ol) {
  margin: 8px 0 12px 18px;
}
.exhibition-rich :deep(li) {
  margin: 6px 0;
  line-height: 1.6;
}
.exhibition-rich :deep(h1),
.exhibition-rich :deep(h2),
.exhibition-rich :deep(h3) {
  margin: 12px 0 8px;
  color: #333;
  font-weight: 600;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  .page-subtitle {
    font-size: 1rem;
  }
  .intro-content {
    padding: 24px;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}
</style>
